//
//  DebtViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/24.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "DebtViewController.h"
#import "DebtTableViewCell.h"
#import "DebtDetailsViewController.h"
#import "CreditorCell.h"
#import "CouldRemoveData.h"
#import "CouldRemoveModel.h"
#import "RemoveingModel.h"
#import "RemovedModel.h"
#import "AcceptedModel.h"

@interface DebtViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _currentPage;
    BOOL _isLastPage;
}

@property (nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation DebtViewController
{
    CouldRemoveData *data;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    NSString *method;
    if (self.index == 0) {
        method = @"Transfer.GetTransferable";
    } else if (self.index == 1) {
            method = @"Transfer.GetCanturn";
        } else if (self.index == 2){
            method = @"Transfer.GetAlreadytransferred";
        }else {
            method = @"Transfer.GetAlreadyundertake";
        }
    [self.dataArr removeAllObjects];
    data = [[CouldRemoveData alloc]init];
    [data getBorrowList:method WithPage:1 Index:self.index];
   
    __weak typeof(self) weakSelf = self;
    data.passDataBlock = ^void(NSMutableArray   *arr) {
        weakSelf.dataArr = arr;
        [self.debtTableView reloadData];
    };
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self debtTableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    self.debtTableView.backgroundColor = [UIColor whiteColor];
    if (self.index == 0) {
        [self refreshPageWithMethod:@"Transfer.GetTransferable"];
    }else if (self.index == 1) {
        [self refreshPageWithMethod:@"Transfer.GetCanturn"];

    }else if (self.index == 2) {
        [self refreshPageWithMethod:@"Transfer.GetAlreadytransferred"];
    }else {
        [self refreshPageWithMethod:@"Transfer.GetAlreadyundertake"];
    }

}


- (UITableView *)debtTableView{
    if (!_debtTableView) {
        _debtTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStylePlain];
        _debtTableView.delegate = self;
        _debtTableView.dataSource = self;
        _debtTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _debtTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _debtTableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_debtTableView];
    }
    
    return _debtTableView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DebtTableViewCell *debtCell = [tableView dequeueReusableCellWithIdentifier:@"DEBTTABLEVIEWCELL"];
    if (debtCell == nil) {
        debtCell = [[[NSBundle mainBundle] loadNibNamed:@"DebtTableViewCell" owner:nil options:nil] lastObject];
    }
    //cell添加阴影
    debtCell.layer.shadowOffset =CGSizeMake(0, 1);
    debtCell.layer.shadowColor = [UIColor grayColor].CGColor;
    debtCell.layer.shadowRadius = 1;
    debtCell.layer.shadowOpacity = 0.5f;
    
    debtCell.selectionStyle = UITableViewCellSelectionStyleNone;
  
    if (self.index == 0) {
        debtCell.projectName.text = [NSString stringWithFormat:@"%@:%@",[[self.dataArr objectAtIndex:indexPath.section] Project],[[self.dataArr objectAtIndex:indexPath.section] Code]];
        
        debtCell.Amount.text = [NSString stringWithFormat:@"投资本金:%.2f",[[[self.dataArr objectAtIndex:indexPath.section] BeingJing] floatValue]];
        debtCell.term.text = [[self.dataArr objectAtIndex:indexPath.section] PayName];
        debtCell.lilv.text = [NSString stringWithFormat:@"投资利息:%.2f",[[[self.dataArr objectAtIndex:indexPath.section] interest] floatValue]];
        debtCell.shouyi.text = [NSString stringWithFormat:@"待收本息:%.2f",[[[self.dataArr objectAtIndex:indexPath.section] Principal] floatValue]];
        debtCell.addDate.text = [[self.dataArr objectAtIndex:indexPath.section] PaymentDate];
        
    }else if (self.index == 1) {
        debtCell.projectName.text = [NSString stringWithFormat:@"%@%@",[[self.dataArr objectAtIndex:indexPath.section] Project],[[self.dataArr objectAtIndex:indexPath.section] Code]];
        debtCell.Amount.text = [NSString stringWithFormat:@"转让金额:%@",[[self.dataArr objectAtIndex:indexPath.section] Ammount]];
        debtCell.term.text = [[self.dataArr objectAtIndex:indexPath.section] PayName];
        debtCell.lilv.text = [NSString stringWithFormat:@"利率:%@",[[self.dataArr objectAtIndex:indexPath.section] Rate]];
        debtCell.shouyi.text = [NSString stringWithFormat:@"进度:%@%%",[[self.dataArr objectAtIndex:indexPath.section] Progress]];
        debtCell.addDate.text = [[self.dataArr objectAtIndex:indexPath.section] PaymentDate];
    }else if (self.index == 2) {
        debtCell.projectName.text = [NSString stringWithFormat:@"%@:%@",[[self.dataArr objectAtIndex:indexPath.section] Project],[[self.dataArr objectAtIndex:indexPath.section] Code]];
        debtCell.Amount.text = [NSString stringWithFormat:@"转让金额:%@",[[self.dataArr objectAtIndex:indexPath.section] Ammount]];
        debtCell.term.text = [[self.dataArr objectAtIndex:indexPath.section] PayName];
        debtCell.state.text = [[self.dataArr objectAtIndex:indexPath.section] State];
        debtCell.lilv.text = [NSString stringWithFormat:@"利率:%@",[[self.dataArr objectAtIndex:indexPath.section] Rate]];
        debtCell.shouyi.text = [NSString stringWithFormat:@"进度:%@%%",[[self.dataArr objectAtIndex:indexPath.section] Progress]];
        debtCell.addDate.text = [[self.dataArr objectAtIndex:indexPath.section] AddDate];
    }else {
        debtCell.projectName.text = [NSString stringWithFormat:@"%@:%@",[[self.dataArr objectAtIndex:indexPath.section] Project],[[self.dataArr objectAtIndex:indexPath.section] Code]];
        debtCell.Amount.text = [NSString stringWithFormat:@"承接金额:%.2f",[[[self.dataArr objectAtIndex:indexPath.section] Ammount] floatValue]];
        debtCell.term.text = [NSString stringWithFormat:@"剩余期数:%@",[[self.dataArr objectAtIndex:indexPath.section] Number]];
        debtCell.state.text = [[self.dataArr objectAtIndex:indexPath.section] State];
        debtCell.lilv.text = [NSString stringWithFormat:@"利率:%.2f",[[[self.dataArr objectAtIndex:indexPath.section] Rate] floatValue]];
        debtCell.shouyi.text = [[self.dataArr objectAtIndex:indexPath.section] PayName];
        debtCell.addDate.text = [[self.dataArr objectAtIndex:indexPath.section] AddDate];
    }
    
    
    
    return debtCell;
   
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    
    return 8;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


//跳到确认转账页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.index == 0) {
        DebtDetailsViewController *debtDetailView = [[DebtDetailsViewController alloc] init];
        debtDetailView.titleName = [NSString stringWithFormat:@"%@:%@",[[self.dataArr objectAtIndex:indexPath.section] Project],[[self.dataArr objectAtIndex:indexPath.section] Code]];
        debtDetailView.BenjinStr = [NSString stringWithFormat:@"%@元",[[self.dataArr objectAtIndex:indexPath.section] BeingJing]];
        debtDetailView.ShouyiStr = [NSString stringWithFormat:@"%@元",[[self.dataArr objectAtIndex:indexPath.section] interest]];
        debtDetailView.AllStr = [NSString stringWithFormat:@"%@元",[[self.dataArr objectAtIndex:indexPath.section] Principal]];
        
        
        
        debtDetailView.BorrowListId = [[self.dataArr objectAtIndex:indexPath.section] BrrowListId];
        debtDetailView.Borrowid = [[self.dataArr objectAtIndex:indexPath.section] Borrowid];
        debtDetailView.TransferableAmount = [[self.dataArr objectAtIndex:indexPath.section] Principal];
        debtDetailView.TransferableLix = [[self.dataArr objectAtIndex:indexPath.section] interest];
        
        
        
        [self.navigationController pushViewController:debtDetailView animated:YES];
    }
}


//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.debtTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                    {
                                        
//                                        [self getBorrowList:method WithPage:1];
        [data getBorrowList:method WithPage:1 Index:self.index];
        [self.debtTableView.mj_header endRefreshing];
                                    }];
    //上拉加载
    self.debtTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.debtTableView.mj_footer endRefreshing];
            return;
        }
        
        [data getBorrowList:method WithPage:_currentPage++ Index:self.index];
        
        [self.debtTableView.mj_footer endRefreshing];
    }];
    
    
}

@end
